package controllers

import (
	"comment/models"
	"time"

	"github.com/astaxie/beego/orm"
)

type IndexController struct {
	BaseController
}

func (c *IndexController) Main() {
	user := c.GetSession("User")
	query := make(map[string]string)
	if user != nil {
		id := user.(models.User).Id
		record := models.GetLastRecordByUserID(id)
		c.Data["record"] = record
		c.Data["total"] = total(id, record.Logtime)
		c.Data["report"] = models.PageReport(query, 0, 0, 10)
	}
	c.Data["PageTitle"] = "首页"
	c.Layout = "layout/layout.html"
	c.TplName = "index.html"
}

func total(user_id int, audit_time time.Time) map[string]interface{} {
	var (
		m     map[string]interface{}
		count int64
	)
	m = make(map[string]interface{})
	o := orm.NewOrm()
	//评论
	qs := o.QueryTable(new(models.Comment))
	count, _ = qs.Filter("audit_state", 0).Filter("audit_time", audit_time).Count()
	m["t0"] = count
	count, _ = qs.Filter("audit_state", 1).Filter("audit_time", audit_time).Filter("user_id", user_id).Count()
	m["t1"] = count
	count, _ = qs.Filter("audit_state", 2).Filter("audit_time", audit_time).Filter("user_id", user_id).Count()
	m["t2"] = count
	//历史
	count, _ = qs.Filter("audit_state", 0).Count()
	m["t3"] = count
	count, _ = qs.Filter("audit_state", 1).Filter("user_id", user_id).Count()
	m["t4"] = count
	count, _ = qs.Filter("audit_state", 2).Filter("user_id", user_id).Count()
	m["t5"] = count

	//举报
	qs = o.QueryTable(new(models.Report))
	count, _ = qs.Filter("audit_status", 0).Filter("audit_time", audit_time).Count()
	m["t6"] = count
	count, _ = qs.Filter("audit_status", 1).Filter("audit_time", audit_time).Filter("audit_user_id", user_id).Count()
	m["t7"] = count
	count, _ = qs.Filter("audit_status", 2).Filter("audit_time", audit_time).Filter("audit_user_id", user_id).Count()
	m["t8"] = count
	//历史
	count, _ = qs.Filter("audit_status", 0).Count()
	m["t9"] = count
	count, _ = qs.Filter("audit_status", 1).Filter("audit_user_id", user_id).Count()
	m["t10"] = count
	count, _ = qs.Filter("audit_status", 2).Filter("audit_user_id", user_id).Count()
	m["t11"] = count
	return m
}
